import {
  // loginCheck,
  logout
} from "@/api/user";
// import { getAuthByEmpNo } from "@/api/auth";
// import store from "@/store";

import {
  getToken,
  setToken,
  removeToken
} from "@/utils/auth";
import {
  resetRouter
} from "@/router";

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    userInfo: {
      username: "",
      remark: "",
      name: "",
      sex: "",
      status: "",
      job: ""
    },
    // 登录角色
    roles: [],
    // 用户权限
    userPerm: []
    // deptNames: []
  };
};

const state = getDefaultState();

const mutations = {
  // 重置 state 所有内容
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState());
  },
  // 设置 token
  SET_TOKEN: (state, token) => {
    state.token = token;
  },

  SET_NAME: (state, name) => {
    state.name = name;
  },
  // 设置导航栏右侧图标地址
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  // 该用户拥有的按钮权限
  SET_USER_PERM: (state, perms) => {
    state.userPerm = perms;
  },
  // 设置用户信息
  SET_USER_INFO: (state, info) => {
    state.userInfo = Object.assign(state.userInfo, info);
  }
};

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    // const {
    //   username,
    //   password
    // } = userInfo;
    commit("SET_TOKEN", "90--0");
    setToken("token");
    // return new Promise((resolve, reject) => {
    //   loginCheck({
    //     username: username.trim(),
    //     password: password
    //   }).then(user => {
    //     const { token, userObj } = user || {};
    //     const { username, remark, name, sex, status, job } = userObj;
    //     console.log(userObj, "userObj");

    //     const userInfo = {
    //       username, remark, name, sex, status, job
    //     };
    //     console.log(userInfo, "userInfo");
    //     // 设置用户信息
    //     commit("SET_USER_INFO", userInfo);
    //     // 设置 token
    //     commit("SET_TOKEN", token);
    //     // 重新登录清空tabs
    //     store.commit("tabs/CLEAR_TAB");
    //     setToken(token);
    //     console.log("tiao ");
    //     resolve();
    //   }).catch(error => {
    //     reject(error);
    //   });
    // });
  },
  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      // const params = {
      //   username: state.userInfo.username,
      //   sysName: "QM"
      // };
      commit("SET_ROLES", ["admin"]);
      commit("SET_USER_PERM", []);
      resolve(["admin"]);
      // getAuthByEmpNo(params).then(res => {
      //   const data = {
      //     roles: [...res?.menuList || ["default"], ...res?.roleList || []]
      //   };
      //   if (!data) {
      //     reject("Verification failed, please Login again.");
      //   }
      //   commit("SET_ROLES", res?.roleList || ["default"]);
      //   commit("SET_USER_PERM", res?.buttonList || []);
      //   resolve(data.roles);
      // });
    });
  },

  // 退出登录
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      const data = {
        empNo: state.userInfo.username
      };
      console.log(data, "state", state.userInfo);
      logout(data).then(() => {
        removeToken(); // must remove  token  first
        resetRouter();
        commit("RESET_STATE");

        resolve();
      }).catch(error => {
        reject(error);
      });
    });
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
